import { service } from '@/services/request';
import type { 
    MenuItem,
    CarouselItem,
    ArticleRecommendItem,
    ArticleItem,
    LastCommentItem,
    LinkItem,
    BloggerInfo,
    ArticleInfo,
    CommentItem,
    UserInfo,
    SessionItem,
    MessageItem,
    NoticeItem
} from '@/services/type';

interface Request<T> {
    /** 状态码 */
    code: number,
    /** 总数量 */
    count?: number,
    /** 消息提醒 */
    msg: string,
    /** 返回数据 */
    data: T
}

interface CursorBaseResp<T> {
    /** 游标（下次翻页带上这参数） */
    cursor: string,
    /** 是否最后一页 */
    isLast:boolean,
    /** 数据列表 */
    list: T
}

/**
 * api接口
 */
export class api {
    /** 公共模块接口 */
    public static common = {
        // 发送邮箱
        toEmail: (param: any) => service.post<any, Request<any>>('/toEmail', param),
        // 获取图片验证码
        getCodeImg: () => service.get<any, Request<{code: string, img: string}>>('/common/code'),
        // 获取RSA算法公钥
        getPublicKey: () => service.get<any, Request<{key: string, value: string}>>("/common/public/key"),
        // 用户登录
        login: (params: any) => service.post<any, Request<{token: string}>>("/login", params),
        // 查询用户是否已注册
        isExistUserInfo: (params: any) => service.get<any, Request<any>>("/isExistUserInfo", { params }),
        // 用户注销
        logout: () => service.get<any, Request<any>>("/logout"),
        // 文件上传
        upload: (params: any) => service.post<any, Request<{uploadUrl: string, downloadUrl: string}>>('/oss/upload/file', params, { headers: { 'Content-Type': 'multipart/form-data' } }),
        // 获取导航栏初始化菜单信息
        getMenuList: () => service.get<any, Request<MenuItem[]>>('/menu'),
        // 获取博客推荐信息
        getRecommendList: () => service.get<any, Request<ArticleRecommendItem[]>>("/recommend"),
        // 获取最新评论信息
        getLastComment: () => service.get<any, Request<LastCommentItem[]>>("/comment/last"),
        // 获取友链信息
        getLinkList: () => service.get<any, Request<LinkItem[]>>("/link")
    }
    /** 首页模块接口 */
    public static home = {
        // 获取轮播图列表信息
        getCarouselList: () => service.get<any, Request<CarouselItem[]>>('/banner'),
        // 获取博主信息
        getBloggerInfo: () => service.get<any, Request<BloggerInfo>>("/blogger")
    }
    /** 文章相关接口 */
    public static article = {
        // 获取文章列表信息
        getArticleList: (param: any) => service.post<any, Request<CursorBaseResp<ArticleItem[]>>>("/article/list", param),
        // 获取文章详情信息
        getArticleInfo: (params: any) => service.get<any, Request<ArticleInfo>>("/article/detail", { params }),
        // 获取当前文章的上一篇文章信息
        getOnArticle: (params: any) => service.get<any, Request<any>>("/article/on", { params }),
        // 获取当前文章的下一篇文章信息
        getUnderArticle: (params: any) => service.get<any, Request<any>>("/article/under", { params }),
        // 点赞
        giveLike: (params: any) => service.post<any, Request<any>>("/great/giveLike", params),
        // 文章收藏
        collect: (params: any) => service.post<any, Request<any>>("/collect", params),
        // 查询用户收藏文章列表信息
        getCollectArticleList: (params: any) => service.post<any, Request<CursorBaseResp<ArticleItem[]>>>("/collect/article/list", params)
    }
    /** 评论相关接口 */
    public static comment = {
        // 评论发布
        release: (params: any) => service.post<any, Request<any>>('/comment/release', params),
        // 获取评论列表信息
        getCommentList: (params: any) => service.post<any, Request<CursorBaseResp<CommentItem[]>>>('/comment/list', params),
        // 获取评论回复列表信息
        getReplyList: (params: any) => service.post<any, Request<CursorBaseResp<CommentItem[]>>>('/comment/reply/list', params),
        // 查询用户评论信息
        queryUserCommentList: (params: any) => service.post<any, Request<CursorBaseResp<LastCommentItem[]>>>('/comment/user/list', params),
        // 删除评论信息（包括主评论和子评论）
        deleteComment: (params: any) => service.post<any, Request<any>>('/comment/del', params)
    }
    /** 用户相关接口 */
    public static user = {
        // 通过用户内码获取用户信息
        getUserInfo: (params: any) => service.get<any, Request<UserInfo>>('/u/get', { params }),
        // 获取登录用户信息
        getInfo: () => service.get<any, Request<UserInfo>>('/user/info'),
        // 上传用户头像
        uploadIcon: (params: any) => service.post<any, Request<any>>('/user/uploadIcon', params, { headers: { 'Content-Type': 'multipart/form-data' } }),
        // 修改用户信息
        updateUserInfo: (params: any) => service.post<any, Request<any>>('/user/update', params),
        // 验证旧邮箱
        validateOldEmail: (params: any) => service.post<any, Request<any>>('/validate_old_email', params),
        // 绑定邮箱
        bindEmail: (params: any) => service.post<any, Request<any>>('/user/email/update', params),
        // 更新密码
        updatePwd: (params: any) => service.post<any, Request<any>>('/user/pwd/update', params),
        // 获取用户表情包列表
        getUserEmojis: () => service.get<any, Request<any>>('/user/emoji/list'),
        // 上传用户表情包
        uploadUserEmoji: (params: any) => service.post<any, Request<any>>('/user/emoji/upload', params, { headers: { 'Content-Type': 'multipart/form-data' } }),
        // 移除用户表情包
        removeUserEmoji: (params: any) => service.get('/user/emoji/remove', { params }),
    }
    /** 用户关注相关接口 */
    public static follow = {
        /** 用户关注或取消关注 */
        follow: (params: any) => service.post<any, Request<any>>('/follow', params),
        /** 查询指定用户关注列表 */
        queryMyFollow: (params: any) => service.post<any, Request<CursorBaseResp<UserInfo[]>>>('/follow/my-follow', params),
        /** 查询指定用户粉丝列表 */
        queryMyFans: (params: any) => service.post<any, Request<CursorBaseResp<UserInfo[]>>>('/follow/my-fans', params)
    }
    /** 提醒通知相关接口 */
    public static notice = {
        // 清除通知提醒未读消息数
        clearUnread: () => service.post<any, Request<any>>('/remind/clear'),
        // 获取提醒通知列表
        getNoticePage: (params: any) => service.post<any, Request<CursorBaseResp<NoticeItem[]>>>('/remind/list', params)
    }
    /** 用户聊天相关接口 */
    public static chat = {
        // 查询聊天会话列表
        querySessionList: (params: any) => service.get<any, Request<CursorBaseResp<SessionItem[]>>>('/chat/session/get', { params }),
        // 检查会话信息
        checkSession: (params: any) => service.get<any, Request<number>>('/chat/session/check', { params }),
        // 获取会话详情信息
        sessionDetail: (params: any) => service.get<any, Request<SessionItem>>('/chat/session/detail', { params }),
        // 查询聊天记录列表
        queryMessageList: (params: any) => service.get<any, Request<CursorBaseResp<MessageItem[]>>>('/chat/msg/list', { params }),
        // 发送聊天消息
        sendMessage: (params: any) => service.post<any, Request<MessageItem>>('/chat/msg/send', params),
        // 删除聊天消息
        deleteMessage: (params: any) => service.post<any, Request<any>>('/chat/msg/delete', params),
        // 清空消息未读数
        clearUnread:(params: any) => service.post<any>('/chat/unread/clear', params),
    }
}